package Database;

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Hai
 */
public class Connect {

    private static Connection conn = null;

    public static Connection ConnectMySQL() {
        String db = "travel"; //Tên databases cần kết nối
        String user = "root"; //Mặc định là root
        String pass = ""; //Mật khẩu quản trị MySQL

        String url = "jdbc:mysql://localhost/" + db + "?user=" + user + "&password=" + pass + "&useUnicode=yes&characterEncoding=UTF-8";
        String Driver = "com.mysql.jdbc.Driver";

        try {
            // tao ket noi CSDL
            Class.forName(Driver);
            conn = (Connection) DriverManager.getConnection(url);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (conn == null) {
            JOptionPane.showMessageDialog(null, 1);
        }

        return conn;
    }

    public static String Login(String username, String password) {
        String ret = null;
        String sSQL = "SELECT id, idUser, level FROM `user` WHERE idUser='" + username + "' and pass='" + password + "' ;";

        try {
            Statement st = (Statement) ConnectMySQL().createStatement();
            ResultSet rs = st.executeQuery(sSQL);
            if (rs.next()) {
                ret = (rs.getString("id") + "-" +rs.getString("idUser") + "-" + rs.getString("level"));
            }

            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
    
    
}
